//ok
namespace hjc {
    export class Tab {
        private $tglBtns: hjc.ToggleButton[];
        private $vstk: eui.ViewStack;
        private $index: number;
        private $ani: boolean;
        private $select: hjc.ToggleButton;

        public constructor(tabs: hjc.ToggleButton[] | eui.Group, vstk?: eui.ViewStack, ani: boolean = false) {
            this.$index = -1;
            if (tabs instanceof eui.Group) this.$tglBtns = ui.getChildren<hjc.ToggleButton>(tabs, hjc.ToggleButton);
            else this.$tglBtns = tabs as hjc.ToggleButton[];
            this.$vstk = vstk;
            this.$ani = ani;
            this.$tglBtns.forEach((tglBtn: hjc.ToggleButton) => tglBtn.tab = this);
        }

        public get tglBtns(): hjc.ToggleButton[] { return this.$tglBtns; }
        public get index(): number { return this.$index; }
        public set index(index: number) {
            if (index >= 0 && this.$tglBtns[index]) {
                this.$select = this.$tglBtns[index];
                this.$select.selected = true;
                this.$index = index;
                if (this.$vstk) this.$vstk.selectedIndex = index;
            }
        }
        public get select(): hjc.ToggleButton { return this.$select; }
        public set select(select: hjc.ToggleButton) {
            let index = this.$tglBtns.indexOf(select);
            if (index < 0 || index === this.$index) return;
            this.$index = index;
            this.$select = select;
            if (this.$vstk && this.$vstk.selectedIndex !== this.$index) {
                this.$vstk.selectedIndex = index;
                if (this.$ani) {
                    if (numeric(this.$vstk.left) || numeric(this.$vstk.right)) console.warn('tab选项面板若需要切换动画，请勿设置left/right属…');
                    else {
                        let card = this.$vstk.getChildAt(index);
                        egret.Tween.removeTweens(card);
                        card.x = card.width;
                        egret.Tween.get(card).to({ x: 0 }, 150, egret.Ease.sineOut);
                    }

                }
            }
            for (let i = 0; i < this.$tglBtns.length; i++) {
                this.$tglBtns[i].selected = (i === this.$index);
            }
        }

        public setToggleBgs(...tglBgs: string[]): Tab {
            tglBgs.forEach((src: string, index: number) => {
                if (this.$tglBtns[index]) this.$tglBtns[index].setToggleBg(src);
            });
            return this;
        }

        public setBgs(...bgs: string[]): Tab {
            bgs.forEach((src: string, index: number) => {
                if (this.$tglBtns[index]) this.$tglBtns[index].setBg(src);
            });
            return this;
        }

        public addToggleModes(...toggleModes: hjc.ToggleMode[]): Tab {
            this.$tglBtns.forEach((tglBtn: ToggleButton) => tglBtn.addToggleModes(...toggleModes));
            return this;
        }
    }
}